//
//  DZHeadOption.m
//  DaoZiJinFu
//
//  Created by dzjf on 2017/1/17.
//  Copyright © 2017年 dzjf. All rights reserved.
//

#import "DZHeadOption.h"

@implementation DZHeadOption

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    [self initView];
}
- (void)initView {
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    NSArray *titleArray = _titleArray;
    NSInteger count = titleArray.count;
    for (NSInteger i = 0; i < count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * width/count, 0, width/count, height);
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];
        [button setTitleColor:BLUE_COLOR forState:UIControlStateSelected];
        button.titleLabel.font =[UIFont systemFontOfSize:15];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button addTarget:self action:@selector(changeFinancialStyle:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 10000 + i;
        [self addSubview:button];
        if (i) {
            UIView *lines = [[UIView alloc] initWithFrame:CGRectMake(i * width/count, 10, 1, height-20)];
            lines.backgroundColor = RGB(230, 230, 230);
            [self addSubview:lines];
        }else{
            button.selected = YES;
        }
    }
    UIView *lines = [[UIView alloc] initWithFrame:CGRectMake(0, height - 1, SCREEN_WIDTH, 1)];
    lines.backgroundColor = RGB(220, 220, 220);
    [self addSubview:lines];
    
    _scrollView = [[UIView alloc] initWithFrame:CGRectMake(0,  height - 2, SCREEN_WIDTH/titleArray.count, 1.5)];
    _smallScroll = [[UIView alloc] init];
    _smallScroll.backgroundColor = BLUE_COLOR;
    [_scrollView addSubview:_smallScroll];
    NSString *titles = _titleArray[0];
    CGSize size = [titles sizeWithAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    [_smallScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_scrollView);
        make.height.mas_equalTo(1.5);
        make.width.mas_equalTo(size.width + 10);
        make.top.equalTo(_scrollView).offset(0);
    }];
    [self addSubview:_scrollView];
}



- (void)changeFinancialStyle:(UIButton *)button {
    for (NSInteger i = 0; i < self.titleArray.count; i ++) {
        UIButton *button = [self viewWithTag:i+10000];
        button.selected = NO;
    }
    NSInteger currect = button.tag - 10000;
    button.selected = YES;
    
    CGRect rect = _scrollView.frame;
    rect.origin.x = SCREEN_WIDTH/_titleArray.count * currect;
    NSString *titles = _titleArray[currect];
    CGSize size = [titles sizeWithAttributes:@{ NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    [_smallScroll mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(size.width + 10);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        _scrollView.frame = rect;
    }];
    if (self.delegate && [self.delegate respondsToSelector:@selector(DZHeadOptionView:currectIndex:)]) {
        [self.delegate DZHeadOptionView:self currectIndex:currect];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
